package com.sdry.web.controller.zc.quality;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcGasCriterionEntity;
import com.sdry.model.zc.ZcGasEntity;
import com.sdry.model.zc.ZcLeakHuntingEntity;
import com.sdry.model.zc.ZcProductTestEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.service.zc.ZcCriterionService;
import com.sdry.service.zc.ZcGasService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcGasController   
 * @Description:  气密性打压
 * @Author:       zc   
 * @CreateDate:   2018年10月9日 下午2:27:15   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("gas")
public class ZcGasController {

	@Resource
	private ZcGasService zcGasService;
	@Resource
	private ZcCriterionService zcCriterionService;
	/**
	 * 进入气密性打压页面
	 * @return
	 */
	@RequestMapping("enterGasPage")
	public String enterGasPage(Model model){
		ZcGasCriterionEntity zcGasCriterionEntity = zcCriterionService.selectGasCriterion();
		model.addAttribute("zcGasCriterionEntity",zcGasCriterionEntity);
		return "/zc/quality/gas";
	}
	/**
	 * 保存气密性测试记录
	 * @param request
	 * @param response
	 * @param zcGasEntity
	 */
	@RequestMapping("insertGas")
	public void insertGas(HttpServletRequest request,HttpServletResponse response,ZcGasEntity zcGasEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcGasEntity.setTestPerson(userName);
		int affact = zcGasService.insertGas(zcGasEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查看今日气密性检测记录
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 * @param httpSession
	 */
	@RequestMapping("selectTodayLogList")
	public void selectTodayLogList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		Date date = new Date();
		zcQueryCriteria.setWorker(userName);
		zcQueryCriteria.setWorkTime(date);
		List<ZcGasEntity> todayLogList = zcGasService.selectTodayLogList(zcQueryCriteria);
		int count = zcGasService.countTodayLogList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(todayLogList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询组件历史检验记录
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 * @param httpSession
	 */
	@RequestMapping("selectHistoryLogList")
	public void selectHistoryLogList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		List<ZcProductTestEntity> historyLogList = zcGasService.selectHistoryLogList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(historyLogList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
